package controllers;

import models.CustomerOrder;
import models.production.ProdRequest;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;

import views.html.production.prodRequest.createRequest;

/**
 * Created by Gipxy group
 * Date: 5/1/12
 */
public class ProdRequestController extends Controller {
    /**
     * This result directly redirect to application home.
     */
    public static Result GO_HOME = redirect(
            routes.ProdRequestController.list(0, "name", "asc", "")
    );

    /**
     * Display the paginated list of computers.
     *
     * @param page Current page number (starts from 0)
     * @param sortBy Column to be sorted
     * @param order Sort order (either asc or desc)
     * @param filter Filter applied on computer names
     */
    public static Result list(int page, String sortBy, String order, String filter) {
        return ok(
                /*
                list.render(
                        Computer.page(page, 10, sortBy, order, filter),
                        sortBy, order, filter
                )
                */
        );
    }

    public static Result createRequest() {
        Form<ProdRequest> modelForm = form(ProdRequest.class);
        Long soLenhSx = ProdRequest.getNextSoLenhSX();
        ProdRequest r = new ProdRequest();
        r.setSoLenhSx(soLenhSx);
        modelForm = modelForm.fill(r);
        return ok(
                createRequest.render(modelForm)
        );
    }

    /**
     * Handle the 'new computer form' submission
     */
    public static Result save() {
        Form<ProdRequest> form = form(ProdRequest.class).bindFromRequest();
        if(form.hasErrors()) {
            return badRequest(createRequest.render(form));
        }
        form.get().save();
        flash("success", "ProdRequest has been created");
        return GO_HOME;
    }
    
    public static Result productCbx() {
        String idStr = request().queryString().get("customerOrderId")[0];
        Long id = Long.parseLong(idStr);
        CustomerOrder o = CustomerOrder.find.byId(id);
        return ok(

            //productCbx.render(o.allProduct)
        );
    }
}
